package com.newhope.eshop.common;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

public class Response<T> implements Serializable {
    private static final long serialVersionUID = -750644833749014618L;
    private T data;

    public void setCode(Integer code) {
        this.code = code;
    }

    private Integer code;

    private String msg;

    @Getter
    @Setter
    private Boolean isSuccess;


    public Integer getCode() {
        return code;
    }

    public Response() {
        this.code = 0;
        this.msg = "请求成功";
        this.setIsSuccess(true);
    }

    public Response(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }



    public static <T> Response<T> ok(T data) {
        Response resp = new Response();
        resp.setCode(0);
        resp.setData(data);
        resp.setIsSuccess(true);
        return resp;
    }


    public static <T> Response<T> fail(Integer code, String error) {
        Response resp = new Response();
        resp.setCode(code);
        resp.setMsg(error);
        resp.setIsSuccess(false);
        return resp;
    }
}